package com.azt.provider.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.azt.api.pojo.Advertise;
import com.azt.api.pojo.AdvertiseImg;
import com.azt.api.pojo.ChannelRecommend;
import com.azt.api.pojo.ContentRecommend;
import com.azt.api.service.AdvertiseService;
import com.azt.api.service.RecommendService;
import com.azt.model.page.Pagination;
import com.azt.provider.mapper.AdvertiseImgMapper;
import com.azt.provider.mapper.AdvertiseMapper;
import com.azt.provider.mapper.ChannelRecommendMapper;
import com.azt.provider.mapper.ContentRecommendMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class RecommendServiceImpl implements RecommendService
{
   
	
	@Autowired
	private ChannelRecommendMapper mapper;
	
	@Autowired
	private ContentRecommendMapper contentMapper;
    
    @Override
    public Pagination getChannelPage(int pageno, int pagesize, Map<String, String> searchMap)
    {
        Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
		List<ChannelRecommend> List = mapper.getChannelByParams(searchMap);
		Pagination p = new Pagination(startPage, List);
		p.setList(List);
		p.setParams(searchMap);
        return p;
        
    }
    
    @Override
    public void saveChannel(ChannelRecommend channel)
    {
        mapper.insert(channel);
    }
    
    @Override
    public void deleteChannelById(Integer id)
    {
    	mapper.deleteByPrimaryKey(id);
    }
    
    @Override
    public void editChannel(ChannelRecommend channel)
    {
    	mapper.updateByPrimaryKeySelective(channel);
    }
    
    @Override
    public boolean findOrtherChannel(ChannelRecommend channel)
    {
        List<ChannelRecommend> channels = null;
        Integer id = channel.getId();
        String code = channel.getCode();
        if (id != null && !"".equals(id))
        {
            Example exp = new Example(ChannelRecommend.class);
            exp.createCriteria().andCondition(" code = '"+ code + "' and id <> "+id);
            channels = mapper.selectByExample(exp);
        }
        else
        {
            Example exp = new Example(ChannelRecommend.class);
            exp.createCriteria().andCondition(" code = '"+ code + "'");
            channels = mapper.selectByExample(exp);
        }
        if (channels.size() > 0)
        {
            return true;
        }
        
        return false;
    }

	@Override
	public Pagination getContentPage(int pageno, int pagesize, Map<String, String> searchMap) {
	      Page<Object> startPage = PageHelper.startPage(pageno, pagesize);
			List<ContentRecommend> List = contentMapper.getContentByParams(searchMap);
			Pagination p = new Pagination(startPage, List);
			p.setList(List);
			p.setParams(searchMap);
	        return p;
	}

	@Override
	public void deleteContentById(Integer id) {
		contentMapper.deleteByPrimaryKey(id);
		
	}

	@Override
	public void saveContent(ContentRecommend content) {
		contentMapper.insert(content);

		
	}

	@Override
	public void editContent(ContentRecommend content) {
		contentMapper.updateByPrimaryKeySelective(content);
		
	}

	@Override
	public List<ChannelRecommend> getChannelList() {
		return mapper.selectAll();
	}
}
